/*
 * @Description: 
 * @Author: yyh
 * @Date: 2022-03-30 00:42:45
 * @LastEditors: yyh
 * @LastEditTime: 2022-05-16 12:02:22
 */
var db = require("../utils/db");
var url = require("url");
var qs = require("querystring");

//新增借用资产数据
addBorrowingInfo = async (req, res) => {
  var {
    asset_id,
    asset_name,
    borrowing_department,
    borrowing_time,
    return_time,
    income,
    status,
    operator
  } = req.body;
  var sql =
    "insert into borrowing (asset_id,asset_name,borrowing_department,borrowing_time,return_time,income,status,operator) values(?,?,?,?,?,?,?,?)";
  var sqlArr = [
    asset_id,
    asset_name,
    borrowing_department,
    borrowing_time,
    return_time,
    income,
    status,
    operator
  ];
  var callBack = async (err, data) => {
    if(err) {
        console.log(err);
        res.send({
            code: 400,
            msg: "新增失败"
        })
    } else {
        res.send({
            code: 200,
            msg: "新增成功",
            data: data
        })
    }
}
db.sqlConnect(sql,sqlArr,callBack);
};

//删除借用资产数据
deleteBorrowing = async (req, res) => {
  var id = req.query[0];
  var sql = "delete from borrowing where id = ?";
  var sqlArr = [id];
  var result = await db.SySqlConnect(sql, sqlArr);
  if (result.affectedRows == 1) {
    res.send({
      code: 200,
      msg: "删除成功"
    });
  } else {
    res.send({
      code: 400,
      msg: "删除失败"
    });
  }
};

//修改借用资产数据
updateBorrowingInfo = async (req, res) => {
  var {
    id,
    asset_id,
    asset_name,
    borrowing_department,
    borrowing_time,
    return_time,
    income,
    operator
  } = req.body;
  var sql =
    "update borrowing set asset_id=?,asset_name=?,borrowing_department=?,borrowing_time=?,return_time=?,income=?,operator=? where id=?";
  var sqlArr = [
    asset_id,
    asset_name,
    borrowing_department,
    borrowing_time,
    return_time,
    income,
    operator,
    id
  ];
  var result = await db.SySqlConnect(sql, sqlArr);
  if (result.affectedRows == 1) {
    res.send({
      code: 200,
      msg: "修改成功"
    });
  } else {
    res.send({
      code: 400,
      msg: "修改失败"
    });
  }
};

//通过id修改借用资产状态
updateBorrowingStatus = async (req, res) => {
  var {
    id,
    status
  } = req.body;
  var sql =
    "update borrowing set status=? where id=?";
  var sqlArr = [
    status,
    id
  ];
  var result = await db.SySqlConnect(sql, sqlArr);
  if (result.affectedRows == 1) {
    res.send({
      code: 200,
      msg: "修改成功"
    });
  } else {
    res.send({
      code: 400,
      msg: "修改失败"
    });
  }
};

//模糊查询、分页查询借用资产数据
getBorrowingInfo = async (req, res) => {
    var params = url.parse(req.url).query;
    params = qs.parse(params);
    var sql = "select * from borrowing";
    var sqlArr = [];
    var isMore = false; //是否有多个查询参数
    if(params.asset_id) {
        sql += " where asset_id like ?";
        sqlArr.push("%" + params.asset_id + "%");
        isMore = true;
    }
    if (params.status) {
      if (isMore) {
        sql += " and status like ?";
      } else {
        sql += " where status like ?";
        isMore = true;
      }
      sqlArr.push("%" + params.status + "%");
    }
    if (params.startTime && params.endTime) {
      if (isMore) {
        sql += " and borrowing_time between ? and ?";
      } else {
        sql += " where borrowing_time between ? and ?";
      }
      sqlArr.push(params.startTime, params.endTime);
    }
    if(params.page && params.pageSize) {
        sql += " limit ?,?";
        sqlArr.push((params.page -1)* params.pageSize, parseInt(params.pageSize));
    }
    var callBack = async (err, data) => {
        if(err) {
            console.log(err);
            res.send({
                code: 400,
                msg: "模糊查询失败"
            })
        } else {
            res.send({
                code: 200,
                msg: "模糊查询成功",
                data: data
            })
        }
    }
    db.sqlConnect(sql,sqlArr,callBack);
}

//通过id查询使用资产数据
getBorrowingInfoById = async (req,res) => {
  var id = req.query[0];
  var sql = "select * from borrowing where id=?";
  var sqlArr = [id];
  var callBack = async (err, data) => {
    if(err) {
        console.log(err);
        res.send({
            code: 400,
            msg: "查询失败"
        })
    } else {
        res.send({
            code: 200,
            msg: "查询成功",
            data: data
        })
    }
}
db.sqlConnect(sql,sqlArr,callBack);
}

module.exports = {
  addBorrowingInfo,
  deleteBorrowing,
  updateBorrowingInfo,
  updateBorrowingStatus,
  getBorrowingInfo,
  getBorrowingInfoById
};